﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics.Contracts;

namespace BlindData
{
	/// <summary>
	/// Generator class that uses a Random class as a base.
	/// </summary>
	public class RandomGenerator : IGenerator
	{
		private Random _random;

		#region Constructors

		public RandomGenerator()
		{
			_random = new Random();
		}

		public RandomGenerator(int seed)
		{
			_random = new Random(seed);
		}

		public RandomGenerator(Random random)
		{
			Contract.Requires(random != null);

			_random = random;
		}

		#endregion

		#region IGenerator Members

		/// <summary>
		/// Returns a nonnegative random number.
		/// </summary>
		/// <returns>A 32-bit signed integer greater than or equal to zero and less than System.Int32.MaxValue.</returns>
		public int Next()
		{
			return _random.Next();
		}

		/// <summary>
		/// Fills the elements of a specified array of bytes with random numbers.
		/// </summary>
		/// <param name="buffer">An array of bytes to contain random numbers.</param>
		public void NextBytes(byte[] buffer)
		{
			_random.NextBytes(buffer);
		}

		/// <summary>
		/// Returns a random number between 0.0 and 1.0.
		/// </summary>
		/// <returns>A double-precision floating point number greater than or equal to 0.0, and less than 1.0.</returns>
		public Probability NextProbability()
		{
			return new Probability(_random.NextDouble());
		}

		#endregion
	}
}
